package servlet;

import core.ServletBase;
import weka.ClassifierHelpper;
import weka.ClustererHelpper;
import weka.CommonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by taohj on 2014/12/19.
 */
@WebServlet("/clustererServlet")
public class ClustererServlet extends ServletBase {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String action = req.getParameter("action");

        if ("input".equals(action)) {
            req.getRequestDispatcher("WEB-INF/page/tabs/cluster/cluster.jsp").forward(req, resp);
        }
        else if ("start".equals(action)) {
            start(req, resp);
        }

    }


    private void start(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String filepath = super.getCurrentFilePath(req);
        String clusterers = req.getParameter("clusterers");
        String option = req.getParameter("option");
       try {


           ClustererHelpper helpper=new ClustererHelpper(filepath,clusterers,option);

           String result = helpper.start().toString();

            result = result.replaceAll(" ", "\u007F ");

            print(resp, result);
        } catch (Exception e) {
            e.printStackTrace();
            print(resp, e.getLocalizedMessage());
        }
    }
}
